1 package activity;
2
3 import java.lang.*;
4 import javax.swing.*;
5 import java.awt.*;
6 import java.awt.event.*;
7 import attr.*;
8
9 public class ManageEmployee extends JFrame implements ActionListener {
10 private JPanel panel;
11 ViewEmployeeActivity prev;
12 private Employee employee;
13 private JButton buttonBack, buttonEdit, buttonDelete;
14 private JLabel title, header, userIdLabel, employeeNameLabel, phoneNumberLabel, roleLabel, salaryLabel;
15 private JTextField userIdTF, employeeNameTF, phoneNumberTF, phoneCodeTF, salaryTF;
16 private JComboBox roleCB;
17
18 public ManageEmployee(String eid, ViewEmployeeActivity prev) {
19 super("Manage Employee");
20
21 this.setSize(500,400);
22 this.setResizable(false);
23 this.setLocationRelativeTo(null);
24 this.prev = prev;
25
26 employee = new Employee(eid);
27 employee.fetch();
28
29 panel = new JPanel();
30 panel.setLayout(null);
31 panel.setBackground(Theme.BACKGROUND_PANEL);
32
33 userIdLabel = new JLabel("Employee ID: "+employee.getUserId());
34 userIdLabel.setBounds(60, 20, 140, 30);
35 userIdLabel.setFont(Theme.FONT_INPUT);
36 panel.add(userIdLabel);
37
38 employeeNameLabel = new JLabel("Name: ");
39 employeeNameLabel.setBounds(60, 60, 140, 30);
40 employeeNameLabel.setFont(Theme.FONT_INPUT);
41 panel.add(employeeNameLabel);
42
43 phoneNumberLabel = new JLabel("Phone: ");
44 phoneNumberLabel.setBounds(60, 100, 140, 30);
45 phoneNumberLabel.setFont(Theme.FONT_INPUT);
46 panel.add(phoneNumberLabel);
47
48 roleLabel = new JLabel("Role: ");
49 roleLabel.setBounds(60, 140, 140, 30);
50 roleLabel.setFont(Theme.FONT_INPUT);
51 panel.add(roleLabel);
52
53 salaryLabel = new JLabel("Salary: ");
54 salaryLabel.setBounds(60, 180, 140, 30);
55 salaryLabel.setFont(Theme.FONT_INPUT);
56 panel.add(salaryLabel);
57
58 employeeNameTF = new JTextField(employee.getEmployeeName());
59 employeeNameTF.setBounds(160, 60, 220, 30);
60 employeeNameTF.setFont(Theme.FONT_INPUT);
61 panel.add(employeeNameTF);
62
63 phoneCodeTF = new JTextField("+880");
64 phoneCodeTF.setEnabled(false);
65 phoneCodeTF.setBounds(160, 100, 40, 30);
66 phoneCodeTF.setFont(Theme.FONT_INPUT);
67 panel.add(phoneCodeTF);
68
69
70 phoneNumberTF = new JTextField(employee.getPhoneNumber().substring(4)+"");
71 phoneNumberTF.setBounds(200, 100, 180, 30);
72 phoneNumberTF.setFont(Theme.FONT_INPUT);
73 panel.add(phoneNumberTF);
74
75 roleCB = new JComboBox(Employee.roles);
76 roleCB.setBounds(160, 140, 160, 30);
77 roleCB.setSelectedIndex(employee.getRole().equals("Manager") ? 1 : 0);
78 roleCB.setFont(Theme.FONT_INPUT);
79 panel.add(roleCB);
80
81 salaryTF = new JTextField(employee.getSalary()+"");
82 salaryTF.setBounds(160, 180, 220, 30);
83 salaryTF.setFont(Theme.FONT_INPUT);
84 panel.add(salaryTF);
85
86 buttonEdit = new JButton("Edit");
87 buttonEdit.setBounds(60, 220, Theme.BUTTON_PRIMARY_WIDTH,30);
88 buttonEdit.setFont(Theme.FONT_BUTTON);
89 buttonEdit.setBackground(Theme.BACKGROUND_BUTTON_PRIMARY);
90 buttonEdit.setForeground(Theme.COLOR_BUTTON_PRIMARY);
91 buttonEdit.addActionListener(this);
92 panel.add(buttonEdit);
93
94 buttonDelete = new JButton("Delete");
95 buttonDelete.setBounds(180, 220, Theme.BUTTON_PRIMARY_WIDTH,30);
96 buttonDelete.setFont(Theme.FONT_BUTTON);
97 buttonDelete.setBackground(Theme.BACKGROUND_BUTTON_PRIMARY);
98 buttonDelete.setForeground(Theme.COLOR_BUTTON_PRIMARY);
99 buttonDelete.addActionListener(this);
100 panel.add(buttonDelete);
101
102 this.add(panel);
103 }
104
105 public void actionPerformed(ActionEvent ae) {
106 if (ae.getSource().equals(buttonEdit)) {
107 try {
108 employee.updateEmployee(employeeNameTF.getText(),Integer.parseInt(phoneNumberTF.getText()),roleCB.getSelectedItem().toString(), Double.parseDouble(salaryTF.getText()));
109 if (!prev.keywordTF.getText().trim().isEmpty())
110 prev.table.setModel(Employee.searchEmployee(prev.keywordTF.getText().trim(), prev.byWhatCB.getSelectedItem().toString()));
111 else
112 prev.table.setModel(Employee.searchEmployee("", "By Name"));
113 this.setVisible(false);
114 }
115 catch (NumberFormatException e) {
116 JOptionPane.showMessageDialog(this,"Invalid Input!");
117 }
118 }
119 else if (ae.getSource().equals(buttonDelete)) {
120 employee.deleteEmployee();
121 if (!prev.keywordTF.getText().trim().isEmpty())
122 prev.table.setModel(Employee.searchEmployee(prev.keywordTF.getText().trim(), prev.byWhatCB.getSelectedItem().toString()));
123 else
124 prev.table.setModel(Employee.searchEmployee("", "By Name"));
125 this.setVisible(false);
126 }
127 else {}
128 }
129 }